<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%String path = request.getContextPath();%>
<%@ taglib uri="/WEB-INF/tld/c.tld" prefix="c" %>
<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <title></title>
    <jsp:include page="../../import.jsp" flush="true"/>
    <style type="text/css">
    </style>
</head>

<body class="iframeBody">
<div class="Wrapper">
    <form id="formSet">
        <div class="sqPanel">
            <div class="btnsBox posL">
                <span id="addBtn" class="sq-button sq-button-round">新增车辆</span>
                <span id="addBtn1" onclick="addBtn1();" class="sq-button sq-button-round">产品挂靠</span>
                <span id="removeBtn" onclick="removeBtn();" class="sq-button sq-button-round">移除挂靠</span>
                <span id="addBtn2" onclick="addSpl();" class="sq-button sq-button-round">关联供应商</span>
                <span id="saveBtn" onclick="save();" class="sq-button sq-button-round">保存</span>
            </div>
            <div class="sqPanel_body" >
                <div id="prjPreAudGrid" class="sq-grid"></div>
            </div>
        </div>
    </form>
</div>
<script>
    var cstNm = '${cstNm}';
    var cstId = '${cstId}';
    var cstTypCd = '${cstTypCd}';
    var prjId = '${prjId}';
    var prjTypCd = '${prjTypCd}';
    var editFlag = true;
    var prjPreAudGrid = null;
    var grid = null;
    var detailFlag = '${detailFlag}';
    var rcCstBscInfoId = '';
    if(detailFlag === 'true'){
        editFlag = false;
    }

    $(function() {

        initGrid(editFlag);

        $('#addBtn').on('click', function(event) {
            var manager = $("#prjPreAudGrid").ligerGetGridManager();
            if (manager.getData().length >= 10){
                top.$.ligerDialog.SQalert({content: "本业务最多允许新增10台车辆",SQ_type: "error"});
            } else {
                manager.addRow({
                    "id": "",
                    "cstNm": cstNm,
                    "prjId": prjId,
                    "cstId": cstId,
                    "vehNo": "",
                    "vin": "",
                    "vinValidRes": "",
                    "vinValidResNm": "",
                    "lnkCompId": "",
                    "lnkCompNm": "",
                    "lnkStarLevel":"",
                    "splId": "",
                    "splNm":"",
                    "isDel": 0,
                    "isHistory": 0,
                });
            }
        });

        initData();
    })

    //初始化列表
    function initGrid(editFlag){
        var params = {prjId:prjId,cstId:cstId};
        params.prjId = prjId;
        grid = $("#prjPreAudGrid").ligerGrid({
            url: '<%=path%>/project/prjPreAudInfo/initList.do',
            method : 'get',
            parms:params,
            columns: [
                { display: 'id', name: 'id', hide:true, width: 1},
                { display: '客户id', name: 'cstId', hide:true, width: 1,
                    render: function () {
                        return cstId;
                    }},
                { display: '客户姓名', name: 'cstNm',align: 'center',
                    render: function () {
                        return cstNm;
                    }},
                { display: 'vin', name: 'vin', editor: {type: 'text',onChanged:validateVin}, enabledSort:true,
                    render: function (rowdata, rowindex, value){
                        var h="";
                        if(detailFlag === 'true' && StringUtils.isNotEmpty(rowdata.vin)){
                            h='<a class="sq-button-hasicon" href="javascript:toRePrjPageList(';
                            h+="'vin','"+rowdata.vin+"'";
                            h+=')"><i class="sqBtnIco_1"></i></a>';
                            h+='&nbsp;&nbsp;&nbsp;&nbsp;'+rowdata.vin;
                        }else{
                            h = rowdata.vin;
                        }
                        return h;
                    }
                },
                { display: 'vin校验结果', name: 'vinValidRes', hide:true, width: 1},
                { display: 'vin校验结果', name: 'vinValidResNm'},
                { display: '车牌号', name: 'vehNo', align: 'center',editor: {type: 'text',onChanged:validateVehNo},},
                { display: '供应商id', name: 'splId', hide: true, width:1},
                { display: '供应商名称', name: 'splNm', enabledSort: true,
                    render: function (rowdata, rowindex, value){
                        var h="";
                        if (rowdata.splId != null && rowdata.splId !== ""){
                            h+='<a href="javascript:splDetail(';
                            h+="'"+rowdata.splId+"'";
                            h+=')">'+value+'</a>';
                        }
                        return h;
                    },
                },
                { display: '供应商星级', name: 'splStarLevel'},
                { display: '挂靠公司id', name: 'lnkCompId', hide: true , width: 1},
                { display: '挂靠公司名称', name: 'lnkCompNm', enabledSort: true,
                    render: function (rowdata, rowindex, value){
                        var h="";
                        if (rowdata.lnkCompId != null && rowdata.lnkCompId !== ""){
                            h+='<a href="javascript:lnkCompDetail(';
                            h+="'"+rowdata.lnkCompId+"'";
                            h+=')">'+value+'</a>';
                        }
                        return h;
                    },
                },
                { display: '挂靠公司星级', name: 'lnkStarLevel'},
                { display: '操作',  enabledSort: true,
                    render: function (rowdata, rowindex, value){
                        if (detailFlag != 'true'){
                            var h='<a class="sq-button-hasicon" href="javascript:removePreInfo(';
                            h+="'"+rowdata.id+"'";
                            h+=",'"+rowindex+"'";
                            h+=')"><i class="sqBtnIco_3"></i>删除</a>'
                            return h;
                        }
                    }
                }
            ],
            pageSize:10,
            root:'rows',
            record:'total',
            hideLoadButton : true,
            width: '100%',
            heightDiff:-2,
            checkbox:true,
            enabledEdit: editFlag,
            allowUnSelectRow: true,
            alternatingRow: true,
            rownumbers:true,
            usePager:false,
        });
        prjPreAudGrid = liger.get("prjPreAudGrid");
    }
    //删除
    function removePreInfo(_id,index) {
        if(_id === null || _id === ""){
            grid.deleteRow(index);
        } else {
            top.$.ligerDialog.confirm("确认删除吗?", function (flag) {
                if (!flag) {
                    return;
                } else {
                    var wait_dialog;
                    $.ajax({
                        type: 'get',
                        dataType: 'json',
                        data: {id:_id},
                        url: '<%=path%>/project/prjPreAudInfo/removePreAudInfo.do',
                        beforeSend: function(){
                            wait_dialog=$.ligerDialog.SQalert({content: LogUtils.LOG_CMM_DATA_LOADING ,SQ_type: "waitting" });
                        },
                        success:function(msg){
                            wait_dialog.close();
                            if(msg.success === 1){
                                rcCstBscInfoId = "";
                                top.$.ligerDialog.SQalert({content: msg.info,SQ_type: "success"});
                                grid.reload();
                            }else{
                                top.$.ligerDialog.SQalert({content: msg.info,SQ_type: "error"});
                            }
                        }
                    });
                }
            });
        }
    }

    function initData(){
        if(detailFlag === 'true'){//详情
            $("#addBtn").hide();
            $("#detailBtn").hide();
            $("#addBtn1").hide();
            $("#removeBtn").hide();
            $("#addBtn2").hide();
            $("#saveBtn").hide();
        }
    }

    //挂靠公司详细信息
    function lnkCompDetail(lnkCmpId){
        if(null===lnkCmpId){
            lnkCmpId = "";
        }
        Add_navTab( 'tab_prj_prd_lnk_detail'+lnkCmpId,'挂靠公司详情','<%=path%>/project/prjlnkcomp/toDetailPage.do?id='+lnkCmpId);
    }

    //供应商详细信息
    function splDetail(splId){
        var url  ="<%=path%>/product/splInfo/toDetailPage.do?id="+splId;
        Add_navTab('tab_spl_detail'+splId,'供应商详细信息',url);
    }

    // 校验车架号vin
    function validateVin(data){
        var record = data.record;
        var vin = record.vin;
        if(StringUtils.isEmpty(vin)){
            prjPreAudGrid.updateCell("vinValidRes",0,record);
            prjPreAudGrid.updateCell("vinValidResNm","未校验",record);
        }else {
            $.ajax({
                type: 'get',
                dataType: 'json',
                data: {vin: vin},
                url: '<%=path%>/project/prjprddtl/validVin.do',
                success: function (msg) {
                    prjPreAudGrid.updateCell("vinValidRes",msg.success,record);
                    prjPreAudGrid.updateCell("vinValidResNm",msg.info,record);
                }
            });
        }
    }

    // 校验车牌号
    function validateVehNo(data){
        var record = data.record;
        var vehNo = record.vehNo;
        if(StringUtils.isEmpty(vehNo)){
            prjPreAudGrid.updateCell("vehNo","",record);
        }else {
            $.ajax({
                type: 'get',
                dataType: 'json',
                data: {vehNo: vehNo},
                url: '<%=path%>/project/prjPreAudInfo/validVehNo.do',
                success: function (msg) {
                    if (msg.success === 1) {
                        prjPreAudGrid.updateCell("vehNo",vehNo,record);
                    } else {
                        $.ligerDialog.SQalert({content: msg.info, SQ_type: "error"});
                        prjPreAudGrid.updateCell("vehNo","",record);
                    }
                }
            });
        }
    }

    //添加挂靠公司
    function addBtn1(){
        var selectedRows = prjPreAudGrid.getSelectedRows();
        if(selectedRows.length != 1){
            $.ligerDialog.SQalert({content: LogUtils.LOG_CMM_SELECT_ONLY_A_ROW, SQ_type: "warring"});
            return;
        }
        var vehNo = selectedRows[0].vehNo;
        if(ProjectConstant.PRJ_TYP_CD_CDDYWUL === prjTypCd){
            if(StringUtils.isEmpty(vehNo)){
                $.ligerDialog.SQalert({content: "车牌号不能为空", SQ_type: "warring"});
                return;
            }
        }
        var _splId = selectedRows[0].splId;
        if ((prjTypCd === 'ESCJYD' || prjTypCd === 'OWNJYD') && StringUtils.isEmpty(_splId)) {
            top.$.ligerDialog.SQalert({content: "请先选择供应商.", SQ_type: "warring"});
            return ;
        }
        var dialogForChooseLnk = $.ligerDialog.open({
            title: '添加产品挂靠公司',
            height: 500,
            width: 850,
            url:'<%=path%>/project/prjPreAudInfo/toAddLnkCompPage.do?prjId='+prjId+'&vehNo='+vehNo+'&prjTypCd='+prjTypCd+'&splId='+_splId,
            onClosed: function () {
                var _lnk = dialogForChooseLnk.get("_lnk");
                var selectedRow = selectedRows[0];
                var rowData = {lnkCompId: _lnk.id, lnkCompNm: _lnk.name, lnkStarLevel: _lnk.star};
                grid.updateRow(selectedRow, rowData);
                if (StringUtils.isNotEmpty(_lnk.rcCstBscInfoId)){
                    rcCstBscInfoId = _lnk.rcCstBscInfoId;
                }
            },
        });
    }

    //移除挂靠
    function removeBtn() {
        var selectedRows = prjPreAudGrid.getSelectedRows();
        if (selectedRows.length < 1) {
            $.ligerDialog.SQalert({content: LogUtils.LOG_CMM_SELECT_MORE_ROW, SQ_type: "warring"});
            return;
        }
        for (var i = 0; i < selectedRows.length; i++) {
            if (selectedRows[i].lnkCompId === null) {
                $.ligerDialog.SQalert({content: "产品无挂靠，请核实后操作！", SQ_type: "warring"});
                return;
            }
            var selectedRow = selectedRows[i];
            var rowData = {lnkCompId: "", lnkCompNm: "", lnkStarLevel: ""};
            rcCstBscInfoId = "";
            grid.updateRow(selectedRow, rowData);
        }
    }
    //关联供应商
    function addSpl(){
        var selectedRows = prjPreAudGrid.getSelectedRows();
        if(selectedRows.length != 1){
            $.ligerDialog.SQalert({content: LogUtils.LOG_CMM_SELECT_ONLY_A_ROW, SQ_type: "warring"});
            return;
        }
        var dialogForChooseSpl = $.ligerDialog.open({
            title: '关联供应商',
            height: 400,
            width: 800,
            url: '<%=path%>/project/prjPreAudInfo/toAddSplPage.do?prjId='+prjId,
            isResize: true ,
            onClosed: function () {
                var _spl = dialogForChooseSpl.get("_spl");
                var selectedRow = selectedRows[0];
                //需要新增星级
                var rowData =  {splId: _spl.id, splNm: _spl.name};
                if (StringUtils.isNotEmpty(_spl.splStarLevel)){
                    rowData = {splId: _spl.id, splNm: _spl.name, splStarLevel: _spl.splStarLevel};
                }
                if (StringUtils.isNotEmpty(_spl.rcCstBscInfoId)){
                    rcCstBscInfoId = _spl.rcCstBscInfoId;
                }
                grid.updateRow(selectedRow, rowData);
            },
        });
    }

    //保存
    function save() {

        var prjPreAudInfoList = prjPreAudGrid.getData();
        for (var i = 0; i < prjPreAudInfoList.length; i++) {
            var _vin = prjPreAudInfoList[i].vin;
            if (StringUtils.isEmpty(_vin)) {
                top.$.ligerDialog.SQalert({content: "vin不能为空.", SQ_type: "error"});
                return false;
            }
            var _vehNo = prjPreAudInfoList[i].vehNo;
            if ((prjTypCd === 'CDDYWUL' || prjTypCd === 'OWNJYD') && StringUtils.isEmpty(_vehNo)) {
                top.$.ligerDialog.SQalert({content: "车牌号不能为空.", SQ_type: "error"});
                return false;
            }
            var _splId = prjPreAudInfoList[i].splId;
            if ((prjTypCd === 'ESCJYD' || prjTypCd === 'OWNJYD') && StringUtils.isEmpty(_splId)) {
                top.$.ligerDialog.SQalert({content: "供应商不能为空.", SQ_type: "error"});
                return false;
            }
        }

        var params = {};
        params.prjId = prjId;
        params.rcCstBscInfoId = rcCstBscInfoId;
        params.prjPreAudInfoList = JSON.stringify(prjPreAudInfoList);
        $.ligerDialog.hide();
        var wait_dialog;
        $.ajax({
            type: 'post',
            dataType: 'json',
            data: params,
            url: '<%=path%>/project/prjPreAudInfo/savePreAudInfo.do',
            beforeSend: function(){
                wait_dialog=$.ligerDialog.SQalert({content: LogUtils.LOG_CMM_DATA_LOADING ,SQ_type: "waitting" });
            },
            success:function(msg){
                wait_dialog.close();
                if(msg.success === 1){
                    top.$.ligerDialog.SQalert({content: msg.info,SQ_type: "success"});
                    grid.reload();
                }else{
                    top.$.ligerDialog.SQalert({content: msg.info,SQ_type: "error"});
                }
            }
        });
    }

    //查询项目信息(查重)
    function toRePrjPageList(type,value){
        var _cstTypCd = '${cstTypCd}';
        if(StringUtils.isEmpty(_cstTypCd)){
            _cstTypCd=1;
        }
        if( StringUtils.isEmpty(prjId) || StringUtils.isEmpty(_cstTypCd))return;
        Add_navTab('tab_re_prj'+'-'+type+cstId,'查重页面','<%=path%>/project/prjbscinfo/toRePrjListPage.do?id='+cstId+'&cstTypCd='+cstTypCd+'&prjId='+prjId+'&type='+type+'&value='+value);
    }
</script>
</body>
</html>
